What is tiny-typed-emitter?
The tiny-typed-emitter package is a lightweight, strongly-typed event emitter for TypeScript and JavaScript. It allows you to create and manage events with type safety, ensuring that event listeners and emitters are correctly typed.
What are tiny-typed-emitter's main functionalities?
Basic Event Emission
This feature allows you to define and emit events with specific types. The code sample demonstrates how to create a custom event emitter, add listeners for 'data' and 'error' events, and emit those events with the appropriate data types.
const { TypedEmitter } = require('tiny-typed-emitter');
class MyEmitter extends TypedEmitter {
// Define event types
on(event: 'data', listener: (data: string) => void): this;
on(event: 'error', listener: (error: Error) => void): this;
}
const emitter = new MyEmitter();
// Add listeners
emitter.on('data', (data) => {
console.log('Data received:', data);
});
emitter.on('error', (error) => {
console.error('Error occurred:', error);
});
// Emit events
emitter.emit('data', 'Hello, World!');
emitter.emit('error', new Error('Something went wrong!'));
Removing Event Listeners
This feature allows you to remove event listeners. The code sample demonstrates how to add a listener for the 'data' event, emit the event, remove the listener, and then emit the event again to show that the listener has been removed.
const { TypedEmitter } = require('tiny-typed-emitter');
class MyEmitter extends TypedEmitter {
on(event: 'data', listener: (data: string) => void): this;
}
const emitter = new MyEmitter();
function onData(data) {
console.log('Data received:', data);
}
// Add listener
emitter.on('data', onData);
// Emit event
emitter.emit('data', 'Hello, World!');
// Remove listener
emitter.off('data', onData);
// Emit event again (no output expected)
emitter.emit('data', 'Hello again!');
Once Event Listeners
This feature allows you to add a listener that will be called only once. The code sample demonstrates how to add a 'once' listener for the 'data' event, emit the event to trigger the listener, and then emit the event again to show that the listener is not called a second time.
const { TypedEmitter } = require('tiny-typed-emitter');
class MyEmitter extends TypedEmitter {
on(event: 'data', listener: (data: string) => void): this;
once(event: 'data', listener: (data: string) => void): this;
}
const emitter = new MyEmitter();
// Add a once listener
emitter.once('data', (data) => {
console.log('Data received:', data);
});
// Emit event (listener will be called)
emitter.emit('data', 'Hello, World!');
// Emit event again (listener will not be called)
emitter.emit('data', 'Hello again!');
Other packages similar to tiny-typed-emitter
eventemitter3
EventEmitter3 is a high-performance event emitter for Node.js and the browser. It is similar to tiny-typed-emitter but does not provide built-in TypeScript support. You can use TypeScript with EventEmitter3, but you will need to define your own type definitions.
mitt
Mitt is a tiny, functional event emitter. It is similar to tiny-typed-emitter in terms of size and simplicity but does not offer built-in TypeScript support. Mitt is designed to be extremely lightweight and fast.
node-event-emitter
Node-Event-Emitter is a simple and lightweight event emitter for Node.js. It is similar to tiny-typed-emitter but does not provide type safety. It is a good choice if you need a basic event emitter without the overhead of type definitions.
tiny-typed-emitter
Have your events and their listeners type-checked with no overhead.
Install
Simply add the dependency using npm:
$ npm i tiny-typed-emitter
or using yarn:
$ yarn add tiny-typed-emitter
Usage
- import tiny-typed-emitter library:
import { TypedEmitter } from 'tiny-typed-emitter';
- define events and their listener signatures (note: quotes around event names are not mandatory):
interface MyClassEvents {
'added': (el: string, wasNew: boolean) => void;
'deleted': (deletedCount: number) => void;
}
- on this step depending on your use case, you can:
Generic events interface
To use with generic events interface:
interface MyClassEvents<T> {
'added': (el: T, wasNew: boolean) => void;
}
class MyClass<T> extends TypedEmitter<MyClassEvents<T>> {
}
Compatible subclasses with different events
The type of eventNames()
is a superset of the actual event names to make
subclasses of a TypedEmitter
that introduce different events type
compatible. For example the following is possible:
class Animal<E extends ListenerSignature<E>=ListenerSignature<unknown>> extends TypedEmitter<{spawn: () => void} & E> {
constructor() {
super();
}
}
class Frog<E extends ListenerSignature<E>> extends Animal<{jump: () => void} & E> {
}
class Bird<E extends ListenerSignature<E>> extends Animal<{fly: () => void} & E> {
}
const animals: Animal[] = [new Frog(), new Bird()];
No Overhead
Library adds no overhead. All it does is it simply reexports renamed EventEmitter
with customized typings.
You can check lib/index.js to see the exported code.